package com.cyt.thrid.wechat.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.lang.reflect.Field;

/**
 * 提供类的一些方法以及反射
 */
public class ClassUtil {

    private static final Logger _log = LoggerFactory.getLogger(ClassUtil.class);

    private static final String SET_METHOD_PREFIX = "set";

    /**
     * 根据给定的对象及属性名，通过set方法对属性赋值
     *
     * @param fieldName      属性名
     * @param fieldValue     属性值
     * @param object          对象
     */
    public static void setObjectFieldValue(final String fieldName, final String fieldValue, Object object) {

        Assert.notNull(object);
        Assert.notNull(fieldName);


        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(object, fieldValue);

            _log.info("对类{}的属性{}赋值成功：{}", object.getClass().getName(), fieldName, fieldValue);
        } catch (IllegalAccessException e) {
            _log.error(e.getMessage());
        } catch (NoSuchFieldException e) {
            _log.error(e.getMessage());
        }
    }


}
